#ifndef CVX_ANN_H
#define CVX_ANN_H 1

//interface for ANN
#include "cvxAnnconfig.h"
#include "ann/ANN.h"

#include <vector>
#include <utility>
using std::vector;
using std::iterator;
using std::pair;

class CvxAnn{
	typedef vector<pair<double, double>> TVecPair;
	typedef TVecPair::iterator TiPair;
	typedef TVecPair::const_iterator TConstIPair;
public:
	//dim: dimension
	//k:   k nearest neighbors
	CvxAnn(const int dim, const int k);
	~CvxAnn();

	// build tree with given data, dimension = 2
	void setTree(const vector<pair<double, double>>& in_pairs);
	// build tree with given data, size: data number
	void setTree(const double *pData, int size);

	//return k nearest neighbor
	void query(const double x, const double y, vector<double> &dis, vector<int> &idx);

	//return k nearest neighbor
	/*
		pData: input vector, length is dimension
		pDis:  output n nearest distance, length is k
		pIdx:  output n index of nearest distance, length is k
	*/
	void query(const double *pData, double *pDis, int *pIdx);

private:
	int					m_nPts;					// actual number of data points
	ANNpointArray		m_dataPts;				// data points
	ANNpoint			m_queryPt;				// query point
	ANNidxArray			m_nnIdx;				// near neighbor indices
	ANNdistArray		m_dists;				// near neighbor distances
	ANNkd_tree*			m_kdTree;				// search structure

	int m_dim;
	int m_k;

};
#endif